package cn.djx.springsecurity.expression;

import cn.djx.springsecurity.entity.LoginUsers;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.List;

@Component("ex")
public class SGExpressionRoot {
    public boolean hasPermission(String authority) {
        // 获取当前登录用户的权限
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUsers loginUser = (LoginUsers) authentication.getPrincipal();
        List<String> permissions = loginUser.getPermission();
        // 判断当前用户是否有权限
        return permissions.contains(authority);
    }
}
